package com.yinhe.module.dr;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhVote;
import com.yinhe.system.service.IYhVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

/**
 * 投票信息Controller
 *
 * @author yinhe
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/dr/vote")
public class DrYhVoteController extends BaseController
{
    @Autowired
    private IYhVoteService yhVoteService;

    /**
     * 查询投票信息列表
     */
    @PreAuthorize("@ss.hasPermi('dr:vote:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhVote yhVote)
    {
        startPage();
        List<YhVote> list = yhVoteService.selectYhVoteList(yhVote);
        return getDataTable(list);
    }

    /**
     * 导出投票信息列表
     */
    @PreAuthorize("@ss.hasPermi('dr:vote:export')")
    @Log(title = "投票信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhVote yhVote)
    {
        List<YhVote> list = yhVoteService.selectYhVoteList(yhVote);
        ExcelUtil<YhVote> util = new ExcelUtil<YhVote>(YhVote.class);
        util.exportExcel(response, list, "投票信息数据");
    }

    /**
     * 获取投票信息详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhVoteService.selectYhVoteById(id));
    }

    /**
     * 新增投票信息
     */
    @PreAuthorize("@ss.hasPermi('dr:vote:add')")
    @Log(title = "投票信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhVote yhVote)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhVote.setUserId(sysUser.getUserId());
        yhVote.setDeptId(sysUser.getDeptId());
        yhVote.setNickName(sysUser.getUserName());
        yhVote.setVoteState("1");//未发布
        return toAjax(yhVoteService.insertYhVote(yhVote));
    }

    /**
     * 修改投票信息
     */
    @PreAuthorize("@ss.hasPermi('dr:vote:edit')")
    @Log(title = "投票信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhVote yhVote)
    {
        return toAjax(yhVoteService.updateYhVote(yhVote));
    }
    @PreAuthorize("@ss.hasPermi('dr:vote:editfb')")
    @Log(title = "投票信息", businessType = BusinessType.UPDATE)
    @PutMapping("/fabu")
    public AjaxResult editFb(@RequestBody YhVote yhVote)
    {

        yhVote.setVoteState("2");//进行中
        return toAjax(yhVoteService.updateYhVote(yhVote));
    }
    @PreAuthorize("@ss.hasPermi('dr:vote:editjs')")
    @Log(title = "投票信息", businessType = BusinessType.UPDATE)
    @PutMapping("/jieshu")
    public AjaxResult editJs(@RequestBody YhVote yhVote)
    {

        yhVote.setVoteState("3");//进行中
        return toAjax(yhVoteService.updateYhVote(yhVote));
    }

    /**
     * 删除投票信息
     */
    @PreAuthorize("@ss.hasPermi('dr:vote:remove')")
    @Log(title = "投票信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhVoteService.deleteYhVoteByIds(ids));
    }

   /* @Scheduled(fixedRate = 60000)//每隔一分钟查一次
    public void CheckVote(@RequestBody YhVote yhVote) {
        List<YhVote> list = (List<YhVote>) yhVoteService.selectYhVoteById(yhVote.getId());
        Timestamp curtime = new Timestamp(System.currentTimeMillis());
        for ( tmp : list) {
            Timestamp tmpfinishtime = tmp.getFinishtime();
            Timestamp tmpstarttime = tmp.getStarttime();
            if (curtime.before(tmpstarttime)) {
                tmp.setState(1);
                examService.modify(tmp);
            }
        }
    }*/
}
